package com.goodfather.textbook.youdao.pad.util;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static android.os.Process.THREAD_PRIORITY_FOREGROUND;

public class ThreadUtil {
    static HandlerThread mHelpThread = new HandlerThread("help_thread", THREAD_PRIORITY_FOREGROUND); //提高优先级
    static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    private static Handler mHelpHandler;
    private static Handler mMainHandler;

    public static void run(Runnable runnable) {
        cachedThreadPool.execute(runnable);
    }

    public synchronized static Handler getMainHandler() {
        if (mMainHandler == null) {
            mMainHandler = new Handler(Looper.getMainLooper());
        }
        return mMainHandler;
    }

    public synchronized static Handler getHelpHandler() {
        if (!mHelpThread.isAlive()) {
            mHelpThread.start();
        }
        if (mHelpHandler == null) {
            mHelpHandler = new Handler(mHelpThread.getLooper());
        }
        return mHelpHandler;
    }
}